package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.CommentDao;
import com.pojo.CommentPojo;
import com.util.JsonUtil;
import com.util.Log;

@WebServlet(urlPatterns={"/user/LoadUserComment"})
public class LoadUserComment extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String pageNumber = request.getParameter("pageNumber");
		String pageSize = request.getParameter("pageSize");
		String caseId = request.getParameter("caseId");
		
		
		// 当前页
		int intPage = Integer.parseInt((pageNumber == null || pageNumber == "0") ? "1"
				: pageNumber);
		// 每页显示条数
		int number = Integer.parseInt((pageSize == null || pageSize == "0") ? "5"
				: pageSize);

		CommentDao dao = new CommentDao();
		String sWhere = " where 1=1 and verifyResult='1' ";
		
		if (caseId != null && !"".equals(caseId))
			sWhere += " and  caseId='" +  Integer.parseInt(caseId) + "'";
		
		List<CommentPojo> list = dao.loadComment(intPage, number, sWhere);

		String sList = "{";
		sList += "\"total\":" + dao.tabCount + ", \"rows\":"; // VoteCount
		sList += JsonUtil.ListToJSON(list);
		sList += "}";
		dao.closeAll();
		Log.write(sList);

		response.setContentType("text/plain");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		out.println(sList);
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
